﻿using Api.DBUtility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Api.DAL
{
    /// <summary>
    /// 数据访问类:evidence_shenpi
    /// </summary>
    public partial class evidence_shenpi
    {
        private string databaseprefix = "eazy_"; //数据库表名前缀
        public evidence_shenpi()
        { }
        #region  BasicMethod ====================================
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from " + databaseprefix + "evidence_shenpi");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.Int,4)
            };
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists_I(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from " + databaseprefix + "evidence_shenpi");
            strSql.Append(" where instance_id=@instance_id");
            SqlParameter[] parameters = {
                    new SqlParameter("@instance_id", SqlDbType.Int,4)
            };
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Models.evidence_shenpi model)
        {
            using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        #region 添加主表数据====================
                        StringBuilder strSql = new StringBuilder();
                        strSql.Append("insert into " + databaseprefix + "evidence_shenpi(");
                        strSql.Append("instance_id,cause,user_name,user_age,user_sex,address_p,mobile_p,daibiaoren,content,bm_remark,bm_qianm,bm_time,zf_remark,zf_qianm,zf_time,fgld_remark,fgld_qianm,fgld_time,add_time,company,address_c,mobile_c)");
                        strSql.Append(" values (");
                        strSql.Append("@instance_id,@cause,@user_name,@user_age,@user_sex,@address_p,@mobile_p,@daibiaoren,@content,@bm_remark,@bm_qianm,@bm_time,@zf_remark,@zf_qianm,@zf_time,@fgld_remark,@fgld_qianm,@fgld_time,@add_time,@company,@address_c,@mobile_c)");
                        strSql.Append(";select @@IDENTITY");
                        SqlParameter[] parameters = {
                                    new SqlParameter("@instance_id", SqlDbType.Int,4),
                                    new SqlParameter("@cause", SqlDbType.NVarChar,255),
                                    new SqlParameter("@user_name", SqlDbType.NVarChar,255),
                                    new SqlParameter("@user_age", SqlDbType.NVarChar,20),
                                    new SqlParameter("@user_sex", SqlDbType.Int,4),
                                    new SqlParameter("@address_p", SqlDbType.NVarChar,255),
                                    new SqlParameter("@mobile_p", SqlDbType.NVarChar,20),
                                    new SqlParameter("@daibiaoren", SqlDbType.NVarChar,20),
                                    new SqlParameter("@content", SqlDbType.NText),
                                    new SqlParameter("@bm_remark", SqlDbType.NText),
                                    new SqlParameter("@bm_qianm", SqlDbType.NVarChar,20),
                                    new SqlParameter("@bm_time", SqlDbType.DateTime),
                                    new SqlParameter("@zf_remark", SqlDbType.NText),
                                    new SqlParameter("@zf_qianm", SqlDbType.NVarChar,20),
                                    new SqlParameter("@zf_time", SqlDbType.DateTime),
                                    new SqlParameter("@fgld_remark", SqlDbType.NText),
                                    new SqlParameter("@fgld_qianm", SqlDbType.NVarChar,20),
                                    new SqlParameter("@fgld_time", SqlDbType.DateTime),
                                    new SqlParameter("@add_time", SqlDbType.DateTime),
                                    new SqlParameter("@company", SqlDbType.NVarChar,255),
                                    new SqlParameter("@address_c", SqlDbType.NVarChar,255),
                                    new SqlParameter("@mobile_c", SqlDbType.NVarChar,20),};
                        parameters[0].Value = model.instance_id;
                        parameters[1].Value = model.cause;
                        parameters[2].Value = model.user_name;
                        parameters[3].Value = model.user_age;
                        parameters[4].Value = model.user_sex;
                        parameters[5].Value = model.address_p;
                        parameters[6].Value = model.mobile_p;
                        parameters[7].Value = model.daibiaoren;
                        parameters[8].Value = model.content;
                        parameters[9].Value = model.bm_remark;
                        parameters[10].Value = model.bm_qianm;
                        parameters[11].Value = model.bm_time;
                        parameters[12].Value = model.zf_remark;
                        parameters[13].Value = model.zf_qianm;
                        parameters[14].Value = model.zf_time;
                        parameters[15].Value = model.fgld_remark;
                        parameters[16].Value = model.fgld_qianm;
                        parameters[17].Value = model.fgld_time;
                        parameters[18].Value = model.add_time;
                        parameters[19].Value = model.company;
                        parameters[20].Value = model.address_c;
                        parameters[21].Value = model.mobile_c;
                        //添加主表数据
                        object obj = DbHelperSQL.GetSingle(conn, trans, strSql.ToString(), parameters); //带事务
                        model.id = Convert.ToInt32(obj);
                        #endregion

                        #region 添加证据列表====================
                        if (model.attach != null)
                        {
                            StringBuilder strSql4;
                            foreach (Models.evidence_shenpi_attach modelt in model.attach)
                            {
                                strSql4 = new StringBuilder();
                                strSql4.Append("insert into " + databaseprefix + "evidence_shenpi_attach(");
                                strSql4.Append("article_id,instance_id,title,guige,danwei,number,remark,add_time)");
                                strSql4.Append(" values (");
                                strSql4.Append("@article_id,@instance_id,@title,@guige,@danwei,@number,@remark,@add_time)");
                                strSql4.Append(";select @@IDENTITY");
                                SqlParameter[] parameters4 = {
                                            new SqlParameter("@article_id", SqlDbType.Int,4),
                                            new SqlParameter("@instance_id", SqlDbType.Int,4),
                                            new SqlParameter("@title", SqlDbType.NVarChar,255),
                                            new SqlParameter("@guige", SqlDbType.NVarChar,255),
                                            new SqlParameter("@danwei", SqlDbType.NVarChar,20),
                                            new SqlParameter("@number", SqlDbType.NVarChar,20),
                                            new SqlParameter("@remark", SqlDbType.NVarChar,500),
                                            new SqlParameter("@add_time", SqlDbType.DateTime)};
                                parameters4[0].Value = model.id;
                                parameters4[1].Value = modelt.instance_id;
                                parameters4[2].Value = modelt.title;
                                parameters4[3].Value = modelt.guige;
                                parameters4[4].Value = modelt.danwei;
                                parameters4[5].Value = modelt.number;
                                parameters4[6].Value = modelt.remark;
                                parameters4[7].Value = modelt.add_time;
                                DbHelperSQL.GetSingle(conn, trans, strSql4.ToString(), parameters4); //带事务
                            }
                        }
                        #endregion

                        #region 修改内容附件==========================
                        //删除已删除的附件
                        new case_attach().DeleteList_I(conn, trans, model.fujian, model.instance_id);
                        // 添加/修改附件
                        if (model.fujian != null)
                        {
                            StringBuilder strSql4;
                            foreach (Models.case_attach modelt in model.fujian)
                            {
                                strSql4 = new StringBuilder();
                                if (modelt.id > 0)
                                {
                                    strSql4.Append("update " + databaseprefix + "case_attach set ");
                                    strSql4.Append("instance_id=@instance_id,");
                                    strSql4.Append("step_id=@step_id,");
                                    strSql4.Append("file_name=@file_name,");
                                    strSql4.Append("file_path=@file_path,");
                                    strSql4.Append("file_size=@file_size,");
                                    strSql4.Append("file_ext=@file_ext,");
                                    strSql4.Append("point=@point,");
                                    strSql4.Append("user_name=@user_name,");
                                    strSql4.Append("article_id=@article_id");
                                    strSql4.Append(" where id=@id");
                                    SqlParameter[] parameters4 = {
                                            new SqlParameter("@instance_id", SqlDbType.Int,4),
                                            new SqlParameter("@step_id", SqlDbType.Int,4),
                                            new SqlParameter("@file_name", SqlDbType.NVarChar,100),
                                            new SqlParameter("@file_path", SqlDbType.NVarChar,255),
                                            new SqlParameter("@file_size", SqlDbType.Int,4),
                                            new SqlParameter("@file_ext", SqlDbType.NVarChar,20),
                                            new SqlParameter("@point", SqlDbType.Int,4),
                                            new SqlParameter("@user_name", SqlDbType.NVarChar,20),
                                            new SqlParameter("@article_id", SqlDbType.Int,4),
                                            new SqlParameter("@id", SqlDbType.Int,4)};
                                    parameters4[0].Value = modelt.instance_id;
                                    parameters4[1].Value = modelt.step_id;
                                    parameters4[2].Value = modelt.file_name;
                                    parameters4[3].Value = modelt.file_path;
                                    parameters4[4].Value = modelt.file_size;
                                    parameters4[5].Value = modelt.file_ext;
                                    parameters4[6].Value = modelt.point;
                                    parameters4[7].Value = modelt.user_name;
                                    parameters4[8].Value = modelt.article_id;
                                    parameters4[9].Value = modelt.id;
                                    DbHelperSQL.ExecuteSql(conn, trans, strSql4.ToString(), parameters4);
                                }
                                else
                                {
                                    strSql4.Append("insert into " + databaseprefix + "case_attach(");
                                    strSql4.Append("instance_id,step_id,file_name,file_path,file_size,file_ext,down_num,point,user_name,article_id)");
                                    strSql4.Append(" values (");
                                    strSql4.Append("@instance_id,@step_id,@file_name,@file_path,@file_size,@file_ext,@down_num,@point,@user_name,@article_id)");
                                    SqlParameter[] parameters4 = {
                                            new SqlParameter("@instance_id", SqlDbType.Int,4),
                                            new SqlParameter("@step_id", SqlDbType.Int,4),
                                            new SqlParameter("@file_name", SqlDbType.NVarChar,100),
                                            new SqlParameter("@file_path", SqlDbType.NVarChar,255),
                                            new SqlParameter("@file_size", SqlDbType.Int,4),
                                            new SqlParameter("@file_ext", SqlDbType.NVarChar,20),
                                            new SqlParameter("@down_num", SqlDbType.Int,4),
                                            new SqlParameter("@point", SqlDbType.Int,4),
                                            new SqlParameter("@user_name", SqlDbType.NVarChar,20),
                                            new SqlParameter("@article_id", SqlDbType.Int,4)};
                                    parameters4[0].Value = modelt.instance_id;
                                    parameters4[1].Value = modelt.step_id;
                                    parameters4[2].Value = modelt.file_name;
                                    parameters4[3].Value = modelt.file_path;
                                    parameters4[4].Value = modelt.file_size;
                                    parameters4[5].Value = modelt.file_ext;
                                    parameters4[6].Value = modelt.down_num;
                                    parameters4[7].Value = modelt.point;
                                    parameters4[8].Value = modelt.user_name;
                                    parameters4[9].Value = modelt.article_id;
                                    DbHelperSQL.ExecuteSql(conn, trans, strSql4.ToString(), parameters4);
                                }
                            }
                        }
                        #endregion

                        trans.Commit();
                    }
                    catch
                    {
                        trans.Rollback();
                        return 0;
                    }
                }
            }
            return model.id;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Models.evidence_shenpi model)
        {
            using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        #region 修改主表数据==========================
                        StringBuilder strSql = new StringBuilder();
                        strSql.Append("update " + databaseprefix + "evidence_shenpi set ");
                        strSql.Append("instance_id=@instance_id,");
                        strSql.Append("cause=@cause,");
                        strSql.Append("user_name=@user_name,");
                        strSql.Append("user_age=@user_age,");
                        strSql.Append("user_sex=@user_sex,");
                        strSql.Append("address_p=@address_p,");
                        strSql.Append("mobile_p=@mobile_p,");
                        strSql.Append("daibiaoren=@daibiaoren,");
                        strSql.Append("content=@content,");
                        strSql.Append("bm_remark=@bm_remark,");
                        strSql.Append("bm_qianm=@bm_qianm,");
                        strSql.Append("bm_time=@bm_time,");
                        strSql.Append("zf_remark=@zf_remark,");
                        strSql.Append("zf_qianm=@zf_qianm,");
                        strSql.Append("zf_time=@zf_time,");
                        strSql.Append("fgld_remark=@fgld_remark,");
                        strSql.Append("fgld_qianm=@fgld_qianm,");
                        strSql.Append("fgld_time=@fgld_time,");
                        strSql.Append("add_time=@add_time,");
                        strSql.Append("company=@company,");
                        strSql.Append("address_c=@address_c,");
                        strSql.Append("mobile_c=@mobile_c");
                        strSql.Append(" where id=@id");
                        SqlParameter[] parameters = {
                                    new SqlParameter("@instance_id", SqlDbType.Int,4),
                                    new SqlParameter("@cause", SqlDbType.NVarChar,255),
                                    new SqlParameter("@user_name", SqlDbType.NVarChar,255),
                                    new SqlParameter("@user_age", SqlDbType.NVarChar,20),
                                    new SqlParameter("@user_sex", SqlDbType.Int,4),
                                    new SqlParameter("@address_p", SqlDbType.NVarChar,255),
                                    new SqlParameter("@mobile_p", SqlDbType.NVarChar,20),
                                    new SqlParameter("@daibiaoren", SqlDbType.NVarChar,20),
                                    new SqlParameter("@content", SqlDbType.NText),
                                    new SqlParameter("@bm_remark", SqlDbType.NText),
                                    new SqlParameter("@bm_qianm", SqlDbType.NVarChar,20),
                                    new SqlParameter("@bm_time", SqlDbType.DateTime),
                                    new SqlParameter("@zf_remark", SqlDbType.NText),
                                    new SqlParameter("@zf_qianm", SqlDbType.NVarChar,20),
                                    new SqlParameter("@zf_time", SqlDbType.DateTime),
                                    new SqlParameter("@fgld_remark", SqlDbType.NText),
                                    new SqlParameter("@fgld_qianm", SqlDbType.NVarChar,20),
                                    new SqlParameter("@fgld_time", SqlDbType.DateTime),
                                    new SqlParameter("@add_time", SqlDbType.DateTime),
                                    new SqlParameter("@company", SqlDbType.NVarChar,255),
                                    new SqlParameter("@address_c", SqlDbType.NVarChar,255),
                                    new SqlParameter("@mobile_c", SqlDbType.NVarChar,20),
                                    new SqlParameter("@id", SqlDbType.Int,4)};
                        parameters[0].Value = model.instance_id;
                        parameters[1].Value = model.cause;
                        parameters[2].Value = model.user_name;
                        parameters[3].Value = model.user_age;
                        parameters[4].Value = model.user_sex;
                        parameters[5].Value = model.address_p;
                        parameters[6].Value = model.mobile_p;
                        parameters[7].Value = model.daibiaoren;
                        parameters[8].Value = model.content;
                        parameters[9].Value = model.bm_remark;
                        parameters[10].Value = model.bm_qianm;
                        parameters[11].Value = model.bm_time;
                        parameters[12].Value = model.zf_remark;
                        parameters[13].Value = model.zf_qianm;
                        parameters[14].Value = model.zf_time;
                        parameters[15].Value = model.fgld_remark;
                        parameters[16].Value = model.fgld_qianm;
                        parameters[17].Value = model.fgld_time;
                        parameters[18].Value = model.add_time;
                        parameters[19].Value = model.company;
                        parameters[20].Value = model.address_c;
                        parameters[21].Value = model.mobile_c;
                        parameters[22].Value = model.id;
                        DbHelperSQL.ExecuteSql(conn, trans, strSql.ToString(), parameters);
                        #endregion

                        #region 修改证据列表==========================
                        //删除已删除的附件
                        new evidence_shenpi_attach().DeleteList(conn, trans, model.attach, model.id);
                        // 添加/修改附件
                        if (model.attach != null)
                        {
                            StringBuilder strSql4;
                            foreach (Models.evidence_shenpi_attach modelt in model.attach)
                            {
                                strSql4 = new StringBuilder();
                                if (modelt.id > 0)
                                {
                                    strSql4.Append("update " + databaseprefix + "evidence_shenpi_attach set ");
                                    strSql4.Append("article_id=@article_id,");
                                    strSql4.Append("instance_id=@instance_id,");
                                    strSql4.Append("title=@title,");
                                    strSql4.Append("guige=@guige,");
                                    strSql4.Append("danwei=@danwei,");
                                    strSql4.Append("number=@number,");
                                    strSql4.Append("remark=@remark");
                                    strSql4.Append(" where id=@id");
                                    SqlParameter[] parameters4 = {
                                                new SqlParameter("@article_id", SqlDbType.Int,4),
                                                new SqlParameter("@instance_id", SqlDbType.Int,4),
                                                new SqlParameter("@title", SqlDbType.NVarChar,255),
                                                new SqlParameter("@guige", SqlDbType.NVarChar,255),
                                                new SqlParameter("@danwei", SqlDbType.NVarChar,20),
                                                new SqlParameter("@number", SqlDbType.NVarChar,20),
                                                new SqlParameter("@remark", SqlDbType.NVarChar,500),
                                                new SqlParameter("@id", SqlDbType.Int,4)};
                                    parameters4[0].Value = modelt.article_id;
                                    parameters4[1].Value = modelt.instance_id;
                                    parameters4[2].Value = modelt.title;
                                    parameters4[3].Value = modelt.guige;
                                    parameters4[4].Value = modelt.danwei;
                                    parameters4[5].Value = modelt.number;
                                    parameters4[6].Value = modelt.remark;
                                    parameters4[7].Value = modelt.id;
                                    DbHelperSQL.ExecuteSql(conn, trans, strSql4.ToString(), parameters4);
                                }
                                else
                                {
                                    strSql4.Append("insert into " + databaseprefix + "evidence_shenpi_attach(");
                                    strSql4.Append("article_id,instance_id,title,guige,danwei,number,remark,add_time)");
                                    strSql4.Append(" values (");
                                    strSql4.Append("@article_id,@instance_id,@title,@guige,@danwei,@number,@remark,@add_time)");
                                    strSql4.Append(";select @@IDENTITY");
                                    SqlParameter[] parameters4 = {
                                            new SqlParameter("@article_id", SqlDbType.Int,4),
                                            new SqlParameter("@instance_id", SqlDbType.Int,4),
                                            new SqlParameter("@title", SqlDbType.NVarChar,255),
                                            new SqlParameter("@guige", SqlDbType.NVarChar,255),
                                            new SqlParameter("@danwei", SqlDbType.NVarChar,20),
                                            new SqlParameter("@number", SqlDbType.NVarChar,20),
                                            new SqlParameter("@remark", SqlDbType.NVarChar,500),
                                            new SqlParameter("@add_time", SqlDbType.DateTime)};
                                    parameters4[0].Value = modelt.article_id;
                                    parameters4[1].Value = modelt.instance_id;
                                    parameters4[2].Value = modelt.title;
                                    parameters4[3].Value = modelt.guige;
                                    parameters4[4].Value = modelt.danwei;
                                    parameters4[5].Value = modelt.number;
                                    parameters4[6].Value = modelt.remark;
                                    parameters4[7].Value = modelt.add_time;
                                    DbHelperSQL.ExecuteSql(conn, trans, strSql4.ToString(), parameters4);
                                }
                            }
                        }
                        #endregion

                        #region 修改内容附件==========================
                        //删除已删除的附件
                        new case_attach().DeleteList_I(conn, trans, model.fujian, model.instance_id);
                        // 添加/修改附件
                        if (model.fujian != null)
                        {
                            StringBuilder strSql4;
                            foreach (Models.case_attach modelt in model.fujian)
                            {
                                strSql4 = new StringBuilder();
                                if (modelt.id > 0)
                                {
                                    strSql4.Append("update " + databaseprefix + "case_attach set ");
                                    strSql4.Append("instance_id=@instance_id,");
                                    strSql4.Append("step_id=@step_id,");
                                    strSql4.Append("file_name=@file_name,");
                                    strSql4.Append("file_path=@file_path,");
                                    strSql4.Append("file_size=@file_size,");
                                    strSql4.Append("file_ext=@file_ext,");
                                    strSql4.Append("point=@point,");
                                    strSql4.Append("user_name=@user_name,");
                                    strSql4.Append("article_id=@article_id");
                                    strSql4.Append(" where id=@id");
                                    SqlParameter[] parameters4 = {
                                            new SqlParameter("@instance_id", SqlDbType.Int,4),
                                            new SqlParameter("@step_id", SqlDbType.Int,4),
                                            new SqlParameter("@file_name", SqlDbType.NVarChar,100),
                                            new SqlParameter("@file_path", SqlDbType.NVarChar,255),
                                            new SqlParameter("@file_size", SqlDbType.Int,4),
                                            new SqlParameter("@file_ext", SqlDbType.NVarChar,20),
                                            new SqlParameter("@point", SqlDbType.Int,4),
                                            new SqlParameter("@user_name", SqlDbType.NVarChar,20),
                                            new SqlParameter("@article_id", SqlDbType.Int,4),
                                            new SqlParameter("@id", SqlDbType.Int,4)};
                                    parameters4[0].Value = modelt.instance_id;
                                    parameters4[1].Value = modelt.step_id;
                                    parameters4[2].Value = modelt.file_name;
                                    parameters4[3].Value = modelt.file_path;
                                    parameters4[4].Value = modelt.file_size;
                                    parameters4[5].Value = modelt.file_ext;
                                    parameters4[6].Value = modelt.point;
                                    parameters4[7].Value = modelt.user_name;
                                    parameters4[8].Value = modelt.article_id;
                                    parameters4[9].Value = modelt.id;
                                    DbHelperSQL.ExecuteSql(conn, trans, strSql4.ToString(), parameters4);
                                }
                                else
                                {
                                    strSql4.Append("insert into " + databaseprefix + "case_attach(");
                                    strSql4.Append("instance_id,step_id,file_name,file_path,file_size,file_ext,down_num,point,user_name,article_id)");
                                    strSql4.Append(" values (");
                                    strSql4.Append("@instance_id,@step_id,@file_name,@file_path,@file_size,@file_ext,@down_num,@point,@user_name,@article_id)");
                                    SqlParameter[] parameters4 = {
                                            new SqlParameter("@instance_id", SqlDbType.Int,4),
                                            new SqlParameter("@step_id", SqlDbType.Int,4),
                                            new SqlParameter("@file_name", SqlDbType.NVarChar,100),
                                            new SqlParameter("@file_path", SqlDbType.NVarChar,255),
                                            new SqlParameter("@file_size", SqlDbType.Int,4),
                                            new SqlParameter("@file_ext", SqlDbType.NVarChar,20),
                                            new SqlParameter("@down_num", SqlDbType.Int,4),
                                            new SqlParameter("@point", SqlDbType.Int,4),
                                            new SqlParameter("@user_name", SqlDbType.NVarChar,20),
                                            new SqlParameter("@article_id", SqlDbType.Int,4)};
                                    parameters4[0].Value = modelt.instance_id;
                                    parameters4[1].Value = modelt.step_id;
                                    parameters4[2].Value = modelt.file_name;
                                    parameters4[3].Value = modelt.file_path;
                                    parameters4[4].Value = modelt.file_size;
                                    parameters4[5].Value = modelt.file_ext;
                                    parameters4[6].Value = modelt.down_num;
                                    parameters4[7].Value = modelt.point;
                                    parameters4[8].Value = modelt.user_name;
                                    parameters4[9].Value = modelt.article_id;
                                    DbHelperSQL.ExecuteSql(conn, trans, strSql4.ToString(), parameters4);
                                }
                            }
                        }
                        #endregion

                        trans.Commit();
                    }
                    catch
                    {
                        trans.Rollback();
                        return false;
                    }
                }
            }
            return true;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            //删除主表
            StringBuilder strSql1 = new StringBuilder();
            strSql1.Append("delete from " + databaseprefix + "evidence_shenpi ");
            strSql1.Append(" where id=@id ");
            SqlParameter[] parameters1 = {
                    new SqlParameter("@id", SqlDbType.Int,4)};
            parameters1[0].Value = id;
            List<CommandInfo> sqllist = new List<CommandInfo>();
            CommandInfo cmd = new CommandInfo(strSql1.ToString(), parameters1);
            sqllist.Add(cmd);

            //删除附表
            StringBuilder strSql2 = new StringBuilder();
            strSql2.Append("delete from " + databaseprefix + "evidence_shenpi_attach ");
            strSql2.Append(" where article_id=@article_id ");
            SqlParameter[] parameters2 = {
                    new SqlParameter("@article_id", SqlDbType.Int,4)};
            parameters2[0].Value = id;
            cmd = new CommandInfo(strSql2.ToString(), parameters2);
            sqllist.Add(cmd);

            int rowsAffected = DbHelperSQL.ExecuteSqlTran(sqllist);
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete_I(int instance_id)
        {
            //删除主表
            StringBuilder strSql1 = new StringBuilder();
            strSql1.Append("delete from " + databaseprefix + "evidence_shenpi ");
            strSql1.Append(" where instance_id=@instance_id ");
            SqlParameter[] parameters1 = {
                    new SqlParameter("@instance_id", SqlDbType.Int,4)};
            parameters1[0].Value = instance_id;
            List<CommandInfo> sqllist = new List<CommandInfo>();
            CommandInfo cmd = new CommandInfo(strSql1.ToString(), parameters1);
            sqllist.Add(cmd);

            //删除附表
            StringBuilder strSql2 = new StringBuilder();
            strSql2.Append("delete from " + databaseprefix + "evidence_shenpi_attach ");
            strSql2.Append(" where instance_id=@instance_id ");
            SqlParameter[] parameters2 = {
                    new SqlParameter("@instance_id", SqlDbType.Int,4)};
            parameters2[0].Value = instance_id;
            cmd = new CommandInfo(strSql2.ToString(), parameters2);
            sqllist.Add(cmd);

            int rowsAffected = DbHelperSQL.ExecuteSqlTran(sqllist);
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Models.evidence_shenpi GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,instance_id,cause,user_name,user_age,user_sex,address_p,mobile_p,daibiaoren,content,bm_remark,bm_qianm,bm_time,zf_remark,zf_qianm,zf_time,fgld_remark,fgld_qianm,fgld_time,add_time,company,address_c,mobile_c ");
            strSql.Append("  from " + databaseprefix + "evidence_shenpi");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.Int,4)
            };
            parameters[0].Value = id;

            Models.evidence_shenpi model = new Models.evidence_shenpi();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Models.evidence_shenpi GetModel_I(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,instance_id,cause,user_name,user_age,user_sex,address_p,mobile_p,daibiaoren,content,bm_remark,bm_qianm,bm_time,zf_remark,zf_qianm,zf_time,fgld_remark,fgld_qianm,fgld_time,add_time,company,address_c,mobile_c ");
            strSql.Append("  from " + databaseprefix + "evidence_shenpi");
            strSql.Append(" where instance_id=@instance_id");
            SqlParameter[] parameters = {
                    new SqlParameter("@instance_id", SqlDbType.Int,4)
            };
            parameters[0].Value = id;

            Models.evidence_shenpi model = new Models.evidence_shenpi();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Models.evidence_shenpi DataRowToModel(DataRow row)
        {
            Models.evidence_shenpi model = new Models.evidence_shenpi();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = int.Parse(row["id"].ToString());
                }
                if (row["instance_id"] != null && row["instance_id"].ToString() != "")
                {
                    model.instance_id = int.Parse(row["instance_id"].ToString());
                }
                if (row["cause"] != null)
                {
                    model.cause = row["cause"].ToString();
                }
                if (row["user_name"] != null)
                {
                    model.user_name = row["user_name"].ToString();
                }
                if (row["user_age"] != null)
                {
                    model.user_age = row["user_age"].ToString();
                }
                if (row["user_sex"] != null && row["user_sex"].ToString() != "")
                {
                    model.user_sex = int.Parse(row["user_sex"].ToString());
                }
                if (row["address_p"] != null)
                {
                    model.address_p = row["address_p"].ToString();
                }
                if (row["mobile_p"] != null)
                {
                    model.mobile_p = row["mobile_p"].ToString();
                }
                if (row["daibiaoren"] != null)
                {
                    model.daibiaoren = row["daibiaoren"].ToString();
                }
                if (row["content"] != null)
                {
                    model.content = row["content"].ToString();
                }
                if (row["bm_remark"] != null)
                {
                    model.bm_remark = row["bm_remark"].ToString();
                }
                if (row["bm_qianm"] != null)
                {
                    model.bm_qianm = row["bm_qianm"].ToString();
                }
                if (row["bm_time"] != null && row["bm_time"].ToString() != "")
                {
                    model.bm_time = DateTime.Parse(row["bm_time"].ToString());
                }
                if (row["zf_remark"] != null)
                {
                    model.zf_remark = row["zf_remark"].ToString();
                }
                if (row["zf_qianm"] != null)
                {
                    model.zf_qianm = row["zf_qianm"].ToString();
                }
                if (row["zf_time"] != null && row["zf_time"].ToString() != "")
                {
                    model.zf_time = DateTime.Parse(row["zf_time"].ToString());
                }
                if (row["fgld_remark"] != null)
                {
                    model.fgld_remark = row["fgld_remark"].ToString();
                }
                if (row["fgld_qianm"] != null)
                {
                    model.fgld_qianm = row["fgld_qianm"].ToString();
                }
                if (row["fgld_time"] != null && row["fgld_time"].ToString() != "")
                {
                    model.fgld_time = DateTime.Parse(row["fgld_time"].ToString());
                }
                if (row["add_time"] != null && row["add_time"].ToString() != "")
                {
                    model.add_time = DateTime.Parse(row["add_time"].ToString());
                }
                if (row["company"] != null)
                {
                    model.company = row["company"].ToString();
                }
                if (row["address_c"] != null)
                {
                    model.address_c = row["address_c"].ToString();
                }
                if (row["mobile_c"] != null)
                {
                    model.mobile_c = row["mobile_c"].ToString();
                }
                model.attach = new evidence_shenpi_attach().GetList(model.id);
                model.fujian = new case_attach().GetList_I(model.instance_id.ToString());
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,instance_id,cause,user_name,user_age,user_sex,address_p,mobile_p,daibiaoren,content,bm_remark,bm_qianm,bm_time,zf_remark,zf_qianm,zf_time,fgld_remark,fgld_qianm,fgld_time,add_time,company,address_c,mobile_c ");
            strSql.Append(" FROM " + databaseprefix + "evidence_shenpi ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,instance_id,cause,user_name,user_age,user_sex,address_p,mobile_p,daibiaoren,content,bm_remark,bm_qianm,bm_time,zf_remark,zf_qianm,zf_time,fgld_remark,fgld_qianm,fgld_time,add_time,company,address_c,mobile_c ");
            strSql.Append(" FROM " + databaseprefix + "evidence_shenpi ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion
    }
}
